<?php
class shopclass {

	function shopclass() {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
		$this->get_all_offs();
	}

	function get_all_offs(){
		global $treeclass;
		if (isset($this->setupdata['modules']['shop']['modules']['off']) && ($this->setupdata['modules']['shop']['modules']['off'] == 1)){
			$sql = $this->mysqlclass->mysql_my_query("
				SELECT `t1`.*, `t2`.`name` `off_describe`, `t2`.`value` `off_value`
				FROM `".$this->setupdata['table_prefix']."product_off_groups` `t1`, `".$this->setupdata['table_prefix']."product_off_groups_data` `t2`
				WHERE `t1`.`id` = `t2`.`parent` AND `t1`.`hide`=0
			", __LINE__, __FILE__);	
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
				$row['name'] = stripslashes($row['name']);
				$row['off_value'] = stripslashes($row['off_value']);
				if ($row['type_of_group'] == 'off1') {
					$this->offs['indivi'][$row['id']][$row['off_describe']] = $row['off_value'];
					$this->offs['indivi'][$row['id']]['name'] = $row['name'];
					$this->offs['indivi'][$row['id']]['id'] = $row['id'];
				} elseif ($row['type_of_group'] == 'off2') {
					$this->offs['select'][$row['id']][$row['off_describe']] = $row['off_value'];
					$this->offs['select'][$row['id']]['name'] = $row['name'];
					$this->offs['select'][$row['id']]['id'] = $row['id'];
					if (isset($_SESSION['select_off']) && ($_SESSION['select_off'] == $row['id'])) $this->offs['select'][$row['id']]['selected'] = 1;
					else $this->offs['select'][$row['id']]['selected'] = 0;
				} elseif ($row['type_of_group'] == 'off3') {
					$this->offs['global'][$row['off_describe']] = $row['off_value'];
					$this->offs['global']['name'] = $row['name'];
					$this->offs['global']['id'] = $row['id'];
				}
			}
			
			$this->offs['select'][0][$row['off_describe']] = '';
			$this->offs['select'][0]['name'] = '';
			$this->offs['select'][0]['num'] = 0;
			$this->offs['select'][0]['id'] = 0;

			if (isset($_SESSION['select_off']) && ($_SESSION['select_off'] > 0) && isset($this->offs['select'])) $this->offs['select'][0]['selected'] = 0;
			else $this->offs['select'][0]['selected'] = 1;
			arsort($this->offs['select']);
			
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_off_depend`", __LINE__, __FILE__);	
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
				if (isset($this->offs['indivi'][$row['off_id']])){
					$this->off_depend[$row['module']][$row['parent']] = $this->offs['indivi'][$row['off_id']];
				} else {
					if ($row['off_id'] == 0) $this->off_depend['adv'][$row['module']][$row['parent']][$row['off_type']] = $row['off_type'];
				}
			}	
			
			if ((isset($_SESSION['client_login_id'])) && (strlen($_SESSION['client_login_id']) > 0)) {		
				global $clientsclass;
				$client_info = $clientsclass->client_info;
				$price_mod = array();
				if (isset($client_info['groups'])){
					foreach($client_info['groups'] as $value){
						$price_mod1 = $this->get_off($value, 'client_group');
						if ($price_mod1 != 0){
							if (isset($price_mod['num'])) {
								if ($price_mod['num'] < $value['num']) $price_mod = $value;
							} else $price_mod = $price_mod1;
						}
					}
					$this->off_client = $price_mod;
					$this->offs['client'] = $this->off_client;
				} else $this->off_client = 0;
			} else $this->off_client = 0;
			if (isset($_SESSION['select_off'])){
				if (isset($this->offs['select'][$_SESSION['select_off']])) $this->off_select = $this->offs['select'][$_SESSION['select_off']];
				else $this->off_select = 0;
			} else $this->off_select = 0;			
		} else $this->offs = 0;
	}
	
	function get_all_types_of_group(){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_discribe` ORDER BY `type`", __LINE__, __FILE__);	
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$result[$row['type']] = $row;
		}
		return $result;
	}

	function product_main_group_add($name){
		if (strlen($name) > 0){
			$name = trim($name);
			$name = addslashes($name);
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_groups` WHERE `name`='".$name."'", __LINE__, __FILE__);		
			if (mysql_num_rows($sql) < 1){
				$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."product_groups` WHERE `parent`=0", __LINE__, __FILE__);						
				if (mysql_num_rows($sql) < 1) $pos = 0;
				else {
					$row = mysql_fetch_array($sql);
					$pos = $row[0] + 1;
				}
				$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_groups` (`id`,`parent`,`name`,`type`,`pos`) VALUES ('', 0, '".$name."', '', ".$pos.")", __LINE__, __FILE__);
				return mysql_insert_id();
			}
		}
		return 0;
	}
	
	function product_group_add($name, $parent, $type){
		if (!isset($parent) or (intval($parent) < 1)) return NULL;
		if ((strlen($name) > 0) and (strlen($type)>0)){
			$parent = intval($parent);
			$name = trim($name);
			$name = addslashes($name);
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_groups` WHERE `name`='".$name."' AND ".$parent."=`parent`", __LINE__, __FILE__);		
			//if (mysql_num_rows($sql) < 1){
				$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."product_groups` WHERE ".$parent."=`parent`", __LINE__, __FILE__);						
				if (mysql_num_rows($sql) < 1) $pos = 0;
				else {
					$row = mysql_fetch_array($sql);
					$pos = $row[0] + 1;
				}
				$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_groups` (`id`,`parent`,`name`,`type`,`pos`) VALUES ('', ".$parent.",'".$name."', '".$type."', ".$pos.")", __LINE__, __FILE__);
				return mysql_insert_id();
			//}
		}
		return 0;
	}
	
	function product_group_del($arr){
		$ids = array();
		foreach($arr as $elem){
			if (isset($elem) and ($elem > 0)){
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_groups` WHERE `id`=".$elem, __LINE__, __FILE__);		
				$row = mysql_fetch_array($sql);
				if ($row['parent'] != 0){
					$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_groups` WHERE ".$elem." = `id`", __LINE__, __FILE__);							
					$sql2 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE `parent`=".$elem, __LINE__, __FILE__);
					while ($row2 = mysql_fetch_array($sql2, MYSQL_ASSOC)) {
						$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_data` WHERE ".$row2['id']." = `parent`", __LINE__, __FILE__);							
					}
				} elseif ($row['parent'] == 0) {
					$sql3 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_groups` WHERE `parent`=".$elem, __LINE__, __FILE__);
						while ($row3 = mysql_fetch_array($sql3, MYSQL_ASSOC)) {
						$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_groups` WHERE ".$row3['id']." = `id`", __LINE__, __FILE__);							
						$sql2 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE `parent`=".$row3['id'], __LINE__, __FILE__);
						while ($row2 = mysql_fetch_array($sql2, MYSQL_ASSOC)) {
							$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_data` WHERE ".$row2['id']." = `parent`", __LINE__, __FILE__);							
						}
						$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE `parent`=".$row3['id'], __LINE__, __FILE__);
					}
				}
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_groups` WHERE `id`=".$elem, __LINE__, __FILE__);
			}
		}
	}
	
	function product_group_mod($parent, $name){
		if (isset($parent) && (intval($parent) > 0)){
			if (!isset($name) && (strlen($name) == 0)) return 5;
			$name = trim($name);
			$name = addslashes($name);
			$parent = intval($parent);
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_groups` WHERE `name`='".$name."' AND ".$parent." = `id`", __LINE__, __FILE__);		
			if (mysql_num_rows($sql) < 1){
				$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_groups` SET `name`='".$name."' WHERE ".$parent." = `id`", __LINE__, __FILE__);
				return 1;
			}
		}
		return 0;
	}
	
	function get_all_product_groups(){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_groups` ORDER BY `pos`", __LINE__, __FILE__);	
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$row['name'] = stripslashes($row['name']);
			$result[$row['id']] = $row;
		}
		return $result;
	}
	
	function get_product_group_data($group_id){
		$result = array();
		$group_id = intval($group_id);
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_groups` WHERE ".$group_id." = `id`", __LINE__, __FILE__);			
		if (mysql_num_rows($sql) > 0) {
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$row['name'] = stripslashes($row['name']);
			$result = $row;
		}
		return $result;
	}
	
	function product_add($name, $parent, $lang){
		if (strlen($name) > 0){
			$name = addslashes(trim($name));
			$parent = intval($parent);
			$info = $this->get_product_group_data($parent);
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$parent." = `parent` ORDER BY `pos` DESC", __LINE__, __FILE__);			
			if (mysql_num_rows($sql) > 0) {
				$row = mysql_fetch_array($sql, MYSQL_ASSOC);
				$pos = $row['pos'] + 1;
			} else $pos = 1;
			$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_catalog` (`id`, `name`, `hide`, `pos`, `parent`, `type`, `time_create`, `time_modif`, `author`, `editor`) VALUES ('', '".$name."', 0, ".$pos.", ".$parent.", '".$info['type']."', '".date("YmdHis")."', '".date("YmdHis")."', '".$_SESSION['login_user']."', '".$_SESSION['login_user']."')", __LINE__, __FILE__);
			$new_id = mysql_insert_id();
			$fields = $this->get_product_group_fields($info['id'], $lang);
			foreach($fields as $array){
				$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_data` (`id`, `parent`, `name`, `value`, `type`, `pos`, `lang`) VALUES ('', ".$new_id.", '".$array['name']."', '', '".$array['type_of_value']."', '".$array['pos']."', '".$lang."')", __LINE__, __FILE__);
			}
			return $new_id;
		}
		return 0;
	}
	
	function get_all_products_by_group_name_only($group_id, $hide="", $curent_page="1", $sort="no"){
		$result = array();
		$group_id = intval($group_id);
		$q = "
			SELECT `t1`.`id` `super_id`, `t1`.`parent` `super_parent`, `t1`.`hide` `super_hide`,`t1`.`name` `super_name`, `t1`.`type`  `super_type`
			FROM `".$this->setupdata['table_prefix']."product_catalog` `t1`
			WHERE ".$group_id." = `t1`.`parent`
		";
		if ($hide == "") $q.= " AND `t1`.`hide`=0";
		$sql = $this->mysqlclass->mysql_my_query($q, __LINE__, __FILE__);
		$num = mysql_num_rows($sql);
		if ($sort != "no") {
			if ($sort == "up") $q.= " ORDER BY `t1`.`name` ASC";
			else $q.= " ORDER BY `t1`.`name` DESC";
		} else {
			$q .= " ORDER by `t1`.`pos`";
		}
		if (isset($this->setupdata['vars']['products_per_page'])) $elements_by_page = $this->setupdata['vars']['products_per_page'];
		else $elements_by_page = 50;
		if ($curent_page == -1) $elements_by_page = 0;
		if ($elements_by_page > 0){
			if ($curent_page == 1) $curent_page = 0;
			else $curent_page = (intval($curent_page)-1)*$elements_by_page;
			$q .= " LIMIT ".$curent_page." , ".$elements_by_page;
		}

		$sql = $this->mysqlclass->mysql_my_query($q, __LINE__, __FILE__);

		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$result[$row['super_id']]['id']= $row['super_id'];
			$result[$row['super_id']]['parent'] = $row['super_parent'];
			$result[$row['super_id']]['name'] = stripslashes($row['super_name']);
			$result[$row['super_id']]['product_name'] = $row['super_name'];
			$result[$row['super_id']]['type'] = $row['super_type'];
			$result[$row['super_id']]['hide'] = $row['super_hide'];
			$result[$row['super_id']]['discount'] = $this->get_price_mod_by_product_id($row['super_id'], 'shop_product', $row['super_parent']);
		}
		
		$result['pages']['all'] = $num;
		if ($elements_by_page > 0) {
			for ($x = 1; $x <= ceil($num/$elements_by_page); $x++) {
				$result['pages']['page'][$x] = $x;
			}
		}
		
		else $result['pages']['all'] = $num;
		if (isset($_GET['page'])) $result['pages']['curent'] = $_GET['page'];
		else $result['pages']['curent'] = 1;

		return $result;
	}
	
	function get_all_products_by_group($group_id, $hide=""){
		$result = array();
		$group_id = intval($group_id);
		$q = "
			SELECT `t1`.`id` `super_id`, `t1`.`parent` `super_parent`, `t1`.`hide` `super_hide`,`t1`.`name` `super_name`, `t1`.`type`  `super_type`, `t2`.`name`, `t2`.`value`, `t2`.`type`, `t2`.`lang`
			FROM `".$this->setupdata['table_prefix']."product_catalog` `t1`, `".$this->setupdata['table_prefix']."product_data` `t2`
			WHERE ".$group_id." = `t1`.`parent` AND `t1`.`id` = `t2`.`parent`
		";
		if ($hide == "") $q.= " AND `t1`.`hide`=0";
		$q .= " ORDER by `t1`.`pos`";
	
		$sql = $this->mysqlclass->mysql_my_query($q, __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$row['name'] = stripslashes($row['name']);
			$row['value'] = stripslashes($row['value']);
			$result[$row['super_id']]['id']= $row['super_id'];
			$result[$row['super_id']]['parent'] = $row['super_parent'];
			$result[$row['super_id']]['name'] = stripslashes($row['super_name']);
			$result[$row['super_id']]['product_name'] = $row['super_name'];
			$result[$row['super_id']]['type'] = $row['super_type'];
			$result[$row['super_id']]['hide'] = $row['super_hide'];
			$result[$row['super_id']]['data'][$row['lang']][$row['name']]['value'] = $row['value'];
			$result[$row['super_id']]['data'][$row['lang']][$row['name']]['type'] = $row['type'];
			$result[$row['super_id']]['discount'] = $this->get_price_mod_by_product_id($row['super_id'], 'shop_product', $row['super_parent']);
			if (($row['type']=='price') && ($row['lang'] == $_SESSION['lang'])){
				if (!isset($row['value']) || ($row['value'] == "")) $row['value'] = 0;
				$result[$row['super_id']]['price'] = $this->get_product_price($row['super_id'], $row['value']);
			}
		}

		foreach($result as $key=>$data){
			if (isset($data['price'])) $price = $data['price'];
			$result[$key] = array_merge($data, $data['data'][$this->setupdata['default_languages']]);
			if (isset($data['data'][$_SESSION['lang']])){
				$tmp = $result[$key];
				$result[$key] = array_merge($tmp, $data['data'][$_SESSION['lang']]);
			}
			unset($result[$key]['data']);
			if (isset($price)) $result[$key]['price'] = $price;
		}
		return $result;
	}
	
	function product_del($arr){
		foreach($arr as $elem){
			if (isset($elem) and ($elem > 0)){
				$sql = $this->mysqlclass->mysql_my_query("DELETE FROM`".$this->setupdata['table_prefix']."product_catalog` WHERE ".$elem." = `id`", __LINE__, __FILE__);		
				$sql = $this->mysqlclass->mysql_my_query("DELETE FROM`".$this->setupdata['table_prefix']."product_data` WHERE ".$elem." = `parent`", __LINE__, __FILE__);		
			}
		}
	}
	
	function gpf_compare($a, $b){
		if ($a['pos'] == $b['pos']) return 0;
		return ($a['pos'] < $b['pos']) ? -1 : 1;
	}
		
	function get_product_fields($product_id){
		if (intval($product_id) < 1) return NULL;
		global $lang_array2;
		$product_id = intval($product_id);
		$result = array(); $result2 = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$product_id." = `id`", __LINE__, __FILE__);					
		if (mysql_num_rows($sql) < 0) return $result;
		$row = mysql_fetch_array($sql, MYSQL_ASSOC);
		$row['name'] = stripslashes($row['name']);
		$parent_info = $row;
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_discribe` WHERE '".$row['type']."'=`type` ORDER BY `pos`", __LINE__, __FILE__);	
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			if ($row['lang'] == '')	$result['all'][$row['id']] = $row;
			else $result[$row['lang']][$row['id']] = $row;
		}
		
		if (!function_exists('gpf_compare')) {
			function gpf_compare($a, $b){
				if ($a['pos'] == $b['pos']) return 0;
				return ($a['pos'] < $b['pos']) ? -1 : 1;
			}
		}
		
		foreach($lang_array2 as $lang)	{
			if ((isset($result[$lang])) && (count($result[$lang])>0)) {
				if ($lang == $this->setupdata['default_languages'])	$result2[$lang] = array_merge($result['all'], $result[$lang]);
				else $result2[$lang] = $result[$lang];
			} else {
				if ($lang == $this->setupdata['default_languages']) $result2[$lang] = $result['all'];
				else $result2[$lang] = array();
			}
			if (isset($result2[$lang]) && is_array($result2[$lang])) usort($result2[$lang], "gpf_compare");
		}
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$product_id." = `id`", __LINE__, __FILE__);					
		$result2['data'] = $parent_info;
		
		return $result2;
	}
	
	function mod_product_fields($parent, $array, $value, $lang){
		if (!isset($parent) or (intval($parent) < 1)) return NULL;
		$parent = intval($parent);
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_data` WHERE ".$parent." = `parent` AND `name`='".$array['name']."' AND `lang`='".$lang."' AND `type`='".$array['type_of_value']."'", __LINE__, __FILE__);					
		if (mysql_num_rows($sql) > 0) {
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_data`  WHERE ".$parent." = `parent` AND `name`='".$array['name']."' AND `lang`='".$lang."' AND `type`='".$array['type_of_value']."'", __LINE__, __FILE__);
		}
		$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_data` (`id`, `parent`, `name`, `value`, `type`, `pos`, `lang`) VALUES ('', ".$parent.", '".$array['name']."', '".$value."', '".$array['type_of_value']."', '".$array['pos']."', '".$lang."')", __LINE__, __FILE__);
	}
	
	function get_product_field_data($parent, $name, $lang){
	    if (!isset($parent) or (intval($parent) < 1)) return NULL;
		$parent = intval($parent);
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_data` WHERE ".$parent." = `parent` AND `name`='".$name."' AND `lang`='".$lang."'", __LINE__, __FILE__);							
		if (mysql_num_rows($sql) > 0) {
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			if ($row['type'] == 'price'){
				$arr = array();
				$cur = $this->get_curency();
				foreach($cur as $key=>$value) {
					if (($value == '') or ($value == 0)) $value = 1;
					$arr[$key] = $row['value']/$value;
				}
				return $arr;
			} else {
				return stripslashes($row['value']);
			}
		} else return "";
		
		
	}
	
	function file_del_with_parent_data($parent, $name, $image_id, $lang){
		//global $fileclass;
		//$fileclass->file_del($image_id);
		if (!isset($parent) or (intval($parent) < 1)) return NULL;
		$parent = intval($parent);
		$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_data` SET `value`='' WHERE ".$parent." = `parent` AND `name`='".$name."' AND `lang`='".$lang."'", __LINE__, __FILE__);
	}


	function get_product_price($product_id, $value){
		if (!isset($product_id) or (intval($product_id) < 1)) return NULL;
		$product_id = intval($product_id);
		if (isset($this->setupdata['modules']['shop']['modules']['off']) && ($this->setupdata['modules']['shop']['modules']['off'] == 1)){
			$discount = $this->get_price_mod_by_product_id($product_id, 'shop_product');
		} else $discount = NULL;
		if (!isset($value) || ($value == "")) $value = 0;
		$result['price']['price'] = $value;
		$result['price']['not_pvm'] = $value;

		if ($discount != NULL){
			$result['price']['discount'] = $discount['num'];
			$result['price']['discount_id'] = $discount['id'];
		}
		
		if (isset($this->setupdata['modules']['shop']['pvm']) && ($value > 0)){
			if (isset($this->setupdata['modules']['shop']['pvm']['custom']) && is_array($this->setupdata['modules']['shop']['pvm']['custom'])){
				$sql = $this->mysqlclass->mysql_my_query("SELECT `type` FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$product_id." = `id`", __LINE__, __FILE__);
				$row = mysql_fetch_array($sql, MYSQL_ASSOC);
				if (isset($this->setupdata['modules']['shop']['pvm']['custom'][$row['type']])) $pvm = $this->setupdata['modules']['shop']['pvm']['custom'][$row['type']];
				else $pvm = $this->setupdata['modules']['shop']['pvm']['default'];
			} else $pvm = $this->setupdata['modules']['shop']['pvm']['default'];
			if (($this->setupdata['modules']['shop']['pvm']['price'] == 'with_pvm') && ($this->setupdata['modules']['shop']['pvm']['off'] == 'without_pvm')){
				$kaina_be_pvm  = $value/(1+$pvm/100);
				if (isset($result['price']['discount'])){
					$nuolaida = $kaina_be_pvm/100*$result['price']['discount'];
					$suma = $kaina_be_pvm - $nuolaida;
					$result['price']['not_pvm'] = $suma;
				} else $suma = $kaina_be_pvm;
				$suma = $suma + $suma/100*$pvm;
			} elseif (($this->setupdata['modules']['shop']['pvm']['price'] == 'without_pvm') && ($this->setupdata['modules']['shop']['pvm']['off'] == 'with_pvm')){
				$kaina_su_pvm = $value+$value*$pvm/100;
				if (isset($result['price']['discount'])){
					$nuolaida = $kaina_su_pvm/100*$result['price']['discount'];
					$suma = $kaina_su_pvm - $nuolaida;
					$result['price']['not_pvm'] = $value - $value/100*$result['price']['discount'];
				} else $suma = $kaina_su_pvm;
				$result['price']['price'] = $kaina_su_pvm;
			} elseif (($this->setupdata['modules']['shop']['pvm']['price'] == 'without_pvm') && ($this->setupdata['modules']['shop']['pvm']['off'] == 'without_pvm')){
				if (isset($result['price']['discount'])) {
					$suma = $value-($value/100*$result['price']['discount']);
					$result['price']['not_pvm'] = $suma;
				} else $suma = $value;
				$suma = $suma + $suma/100*$pvm;
				$result['price']['price'] = $value+$value*$pvm/100;
			} else {
				if (isset($result['price']['discount'])) $suma = $value-($value/100*$result['price']['discount']);
				else $suma = $value;
			}
			$result['price']['total'] = round($suma, 2);
		} else {
			if (isset($result['price']['discount'])) $suma = $value-($value/100*$result['price']['discount']);
			else $suma = $value;
			$result['price']['total'] = round($suma, 2);
		}

		foreach($result['price'] as $key=>$value) $result['price'][$key] = round($value, 2);
		return $result['price'];
	}
		
	function get_product_fields_with_data($product_id, $sel_lang='0'){
		if (intval($product_id) < 1) return NULL;
		$results = array();
		$product_id = intval($product_id);
		$q = "
			SELECT `t1`.`lang` `product_lang`, `t2`.`parent` `product_parent`, `t2`.`type` `product_type`, `t2`.`name` `product_name`, `t1`.*
			FROM `".$this->setupdata['table_prefix']."product_data` `t1`, `".$this->setupdata['table_prefix']."product_catalog` `t2`
			WHERE ".$product_id." = `t2`.`id` AND ".$product_id." = `t1`.`parent`
		";
		$sql = $this->mysqlclass->mysql_my_query($q, __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$row['value'] = stripslashes($row['value']);
			$result['data'][$row['product_lang']][$row['name']] = $row['value'];
			if (!isset($result['product_name'])) $result['product_name'] = stripslashes($row['product_name']);
			if (!isset($result['product_type'])) $result['product_type'] = $row['product_type'];
			if (!isset($result['group_id'])) $result['group_id'] = $row['product_parent'];
			if (($row['type']=='price') /*&& ($row['product_lang'] == $_SESSION['lang'])*/){
				$result['price'] = $this->get_product_price($row['parent'], $row['value']);
			}
		}
		if (isset($result)){
			$results = array_merge($result, $result['data'][$this->setupdata['default_languages']]);
			if (isset($result['data'][$_SESSION['lang']])){
				$tmp = $results;
				$results = array_merge($tmp, $results['data'][$_SESSION['lang']]);
			}
			unset($results['data']);
		}
		if (isset($result['price'])) $results['price'] = $result['price'];

		return $results;
	}
	
	function get_product_fields_with_data_and_type($product_id, $sel_lang='0'){
		if (intval($product_id) < 1) return NULL;
		$results = array();
		$product_id = intval($product_id);
		$q = "
			SELECT `t1`.`lang` `product_lang`, `t2`.`parent` `product_parent`, `t2`.`type` `product_type`, `t2`.`name` `product_name`, `t1`.*
			FROM `".$this->setupdata['table_prefix']."product_data` `t1`, `".$this->setupdata['table_prefix']."product_catalog` `t2`
			WHERE ".$product_id." = `t2`.`id` AND ".$product_id." = `t1`.`parent`
		";
		$sql = $this->mysqlclass->mysql_my_query($q, __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			if (!isset($result['product_name'])) $result['product_name'] = stripslashes($row['product_name']);
			if (!isset($result['product_type'])) $result['product_type'] = $row['product_type'];
			if (!isset($result['group_id'])) $result['group_id'] = $row['product_parent'];
			$row['value'] = stripslashes($row['value']);
			$result['data'][$row['product_lang']][$row['name']]['value'] = $row['value'];
			$result['data'][$row['product_lang']][$row['name']]['type'] = $row['type'];
			if (($row['type']=='price') /*&& ($row['product_lang'] == $_SESSION['lang'])*/){
				$result['price'] = $this->get_product_price($row['parent'], $row['value']);
			}
		}
		if (isset($result)){
			$results = array_merge($result, $result['data'][$this->setupdata['default_languages']]);
			if (isset($result['data'][$_SESSION['lang']])){
				$tmp = $results;
				$results = array_merge($tmp, $results['data'][$_SESSION['lang']]);
			}
			if (isset($result['price'])) $results['price'] = $result['price'];
			else $result['price'] = 0;
			
			$results['main_group'] = $result['price'];
			
			unset($results['data']);
		}
		return $results;
	}
	
	function product_poschange($parent, $array){
		$parent = intval($parent);
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$parent." = `id`", __LINE__, __FILE__);
		if (mysql_num_rows($sql) > 0){
			$data = mysql_fetch_array($sql, MYSQL_ASSOC);
			$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_catalog` SET `pos`=`pos`+1 WHERE `parent` = ".$data['parent']." AND `pos`>".$data['pos'], __LINE__, __FILE__);
			$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_catalog` SET `pos`=".$data['pos']."+1 WHERE `parent` = ".$data['parent']." AND `id`=".$array, __LINE__, __FILE__);
		}
	}
	
	function product_pos($group_id, $subject_id, $pos){
		$group_id = intval($group_id);
		$subject_id = intval($subject_id);
		if ($pos == 'up') {
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$subject_id." = `id`", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0){
				$subject1 = mysql_fetch_array($sql, MYSQL_ASSOC);
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$group_id." = `parent` AND `pos` < ".$subject1['pos']." ORDER BY `pos` DESC", __LINE__, __FILE__);
				if (mysql_num_rows($sql) > 0){
					$subject2 = mysql_fetch_array($sql, MYSQL_ASSOC);
					if ((isset($subject1['pos'])) and (isset($subject2['pos'])) and (strlen($subject1['pos'])>0) and (strlen($subject2['pos'])>0)){
						$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_catalog` SET `pos`=".$subject2['pos']." WHERE `id` = ".$subject1['id'], __LINE__, __FILE__);
						$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_catalog` SET `pos`=".$subject1['pos']." WHERE `id` = ".$subject2['id'], __LINE__, __FILE__);
					}
				}
			}
		} elseif ($pos == 'down'){
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$subject_id." = `id`", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0){
				$subject1 = mysql_fetch_array($sql, MYSQL_ASSOC);
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$group_id." = `parent` AND `pos` > ".$subject1['pos']." ORDER BY `pos` ASC", __LINE__, __FILE__);
				if (mysql_num_rows($sql) > 0){
					$subject2 = mysql_fetch_array($sql, MYSQL_ASSOC);
					if ((isset($subject1['pos'])) and (isset($subject2['pos'])) and (strlen($subject1['pos'])>0) and (strlen($subject2['pos'])>0)){
						$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_catalog` SET `pos`=".$subject2['pos']." WHERE `id` = ".$subject1['id'], __LINE__, __FILE__);
						$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_catalog` SET `pos`=".$subject1['pos']." WHERE `id` = ".$subject2['id'], __LINE__, __FILE__);
					}
				}
			}		
		}
	}
	
	function product_hide($product_id, $hide){
		$product_id = intval($product_id);
		$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_catalog` SET `hide`=".$hide." WHERE `id` = ".$product_id, __LINE__, __FILE__);
	}
	
	function product_copy_to_group($parent, $array){
		$parent = intval($parent);
		foreach($array as $elem){
			if (isset($elem) and ($elem > 0)){
				$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$parent." = `parent`", __LINE__, __FILE__);
				if (mysql_num_rows($sql) < 1) $pos = 0;
				else {
					$row = mysql_fetch_array($sql);
					$pos = $row[0] + 1;
				}
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$elem." = `id`", __LINE__, __FILE__);
				if (mysql_num_rows($sql) == 1){
					$row = mysql_fetch_array($sql, MYSQL_ASSOC);
					$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_catalog` (`id`, `name`, `hide`, `pos`, `parent`, `type`, `time_create`, `time_modif`, `author`, `editor`) VALUES ('', '".mysql_escape_string($row['name'])."', ".$row['hide'].", ".$pos.", ".$parent.", '".$row['type']."', '".date("YmdHis")."', '".date("YmdHis")."', '".$_SESSION['login_user']."', '".$_SESSION['login_user']."')", __LINE__, __FILE__);
					$product_id = mysql_insert_id();		
					$sql3 = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_data` WHERE ".$elem." = `parent`", __LINE__, __FILE__);
					while ($row3 = mysql_fetch_array($sql3, MYSQL_ASSOC)) {
						$sql4 = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."product_data` WHERE ".$product_id." = `parent`", __LINE__, __FILE__);
						if (mysql_num_rows($sql4) < 1) $pos = 0;
						else {
							$row4 = mysql_fetch_array($sql4);
							$pos = $row4[0] + 1;
						}
						$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_data` (`id`, `parent`, `name`, `value`, `type`, `pos`, `lang`) VALUES ('', ".$product_id.", '".$row3['name']."', '".mysql_escape_string($row3['value'])."', '".$row3['type']."', '".$pos."', '".$row3['lang']."')", __LINE__, __FILE__);
					}
				}
			}
		}
	}

	function product_move_to_group($parent, $array){
		$parent = intval($parent);
		foreach($array as $elem){
			if (isset($elem) and ($elem > 0)){
				$sql = $this->mysqlclass->mysql_my_query("SELECT MAX(`pos`) FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$parent." = `parent`", __LINE__, __FILE__);
				if (mysql_num_rows($sql) < 1) $pos = 0;
				else {
					$row = mysql_fetch_array($sql);
					$pos = $row[0] + 1;
				}
				$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_catalog` SET `parent`=".$parent.", `pos`=".$pos." WHERE `id` = ".$elem, __LINE__, __FILE__);
			}
		}
	}
	
	function get_product_group_fields($group_id, $lang=""){
		$result = array();
		$info = $this->get_product_group_data($group_id);
		if ($lang == "") $sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_discribe` WHERE '".$info['type']."' = `type` ORDER BY `pos`", __LINE__, __FILE__);
		else $sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_discribe` WHERE '".$info['type']."' = `type` AND (`lang`='' OR `lang`='".$lang."') ORDER BY `pos`", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) $result[] = $row;
		return $result;
	}
	
	function get_curency(){
		$curency = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name` LIKE 'curency_%'", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$chars = preg_split('/_/', $row['name'], -1, PREG_SPLIT_OFFSET_CAPTURE);
			$result[$chars[1][0]] = $row['value'];
		}
		if ((isset($this->setupdata['vars']['curency']['all'])>0) && (count($this->setupdata['vars']['curency']['all'])>0)){
			foreach($this->setupdata['vars']['curency']['all'] as $key=>$elem){
				if (isset($result[$elem])) $curency[$elem] = $result[$elem];
				else $curency[$elem] = 0;
			}
		}
		return $curency;
	}
	
	function mod_curency($array){
		foreach($array as $key=>$value){
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='curency_".$key."'", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0) {
				$row = mysql_fetch_array($sql, MYSQL_ASSOC);
				$sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."local_vars` SET `value`='".floatval($value)."' WHERE ".$row['id']." = `id`", __LINE__, __FILE__);
			} else {
				$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` (`id`, `name`, `value`) VALUES ('', 'curency_".$key."', '".floatval($value)."')", __LINE__, __FILE__);
			}
		}
	}
	
	function off_set_hide($id, $hide){
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_off_groups` WHERE `id`=".$id, __LINE__, __FILE__);		
		if (mysql_num_rows($sql) > 0) {
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			if ($row['type_of_group'] == 'off3'){
				if ($hide == 1){
					$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_off_groups` SET `hide`=1 WHERE ".$id." = `id`", __LINE__, __FILE__);
				} else {
					$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_off_groups` SET `hide`=1 WHERE `type_of_group`='off3'", __LINE__, __FILE__);
					$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_off_groups` SET `hide`=0 WHERE ".$id." = `id`", __LINE__, __FILE__);
				}
			} else {
				$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_off_groups` SET `hide`=".$hide." WHERE ".$id." = `id`", __LINE__, __FILE__);
			}
		}
	}
	
	function off_main_group_add($name, $type_of_group){
		if (strlen($name) > 0){
			$name = trim($name);
			$name = addslashes($name);
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_off_groups` WHERE `name`='".$name."' AND `type_of_group`='".$type_of_group."'", __LINE__, __FILE__);		
			if (mysql_num_rows($sql) < 1){
				$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_off_groups` (`id`,`hide`,`name`,`type_of_group`) VALUES ('', 1, '".$name."',  '".$type_of_group."')", __LINE__, __FILE__);
				if ($sql) return 0;
			} else return 17;
		} else return 15;
	}

	function get_all_off_groups($type='0'){
		$result = array();
		if ($type == '0') $sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_off_groups` ORDER BY `name`", __LINE__, __FILE__);	
		else $sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_off_groups` WHERE `type_of_group`='".$type."' ORDER BY `name`", __LINE__, __FILE__);	
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$row['name'] = stripslashes($row['name']);
			$result[$row['id']] = $row;
		}
		return $result;
	}
	
	function off_group_del($arr){
		$ids = array();
		foreach($arr as $elem){
			if (isset($elem) and ($elem > 0)){
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_off_groups` WHERE ".$elem." = `id`", __LINE__, __FILE__);		
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_off_groups_data` WHERE ".$elem." = `parent`", __LINE__, __FILE__);		
			}
		}
	}
	
	function mod_off_data($parent, $off){
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_off_groups` WHERE `id`=".$parent, __LINE__, __FILE__);	
		if (mysql_num_rows($sql) > 0){
			$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_off_groups_data` WHERE `parent`=".$parent, __LINE__, __FILE__);	
			$off['desc'] = trim($off['desc']);
			$off['desc'] = addslashes($off['desc']);
			if (strlen($off['desc']) > 0) $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_off_groups_data` (`id`,`parent`,`name`,`value`) VALUES ('', ".$parent.", 'desc', '".$off['desc']."')", __LINE__, __FILE__);
			$off['num'] = floatval($off['num']);
			$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_off_groups_data` (`id`,`parent`,`name`,`value`) VALUES ('', ".$parent.", 'num', '".$off['num']."')", __LINE__, __FILE__);
		}
	}
	
	
	function get_off_data($parent){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_off_groups` WHERE `id`=".$parent, __LINE__, __FILE__);	
		if (mysql_num_rows($sql) > 0){
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$result['name'] = stripslashes($row['name']);
		}
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_off_groups_data` WHERE `parent`=".$parent, __LINE__, __FILE__);	
		if (mysql_num_rows($sql) > 0){
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) $result[$row['name']] = stripslashes($row['value']);
		}
		return $result;
	}
	
	
	function set_off($parent, $off_id, $type){
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_off_depend` WHERE `parent`=".$parent." AND `module`='".$type."'", __LINE__, __FILE__);	
		if ($off_id['indivi'] > 0){
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_off_groups` WHERE `id`=".$off_id['indivi'], __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0){
				$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_off_depend` (`id`,`off_id`,`parent`,`module`, `off_type`) VALUES ('', ".$off_id['indivi'].", ".$parent.", '".$type."', 'off1')", __LINE__, __FILE__);
			}
		}
		if (!isset($off_id['select'])){
			$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_off_depend` (`id`,`off_id`,`parent`,`module`, `off_type`) VALUES ('', 0, ".$parent.", '".$type."', 'off2')", __LINE__, __FILE__);
		}
		if (!isset($off_id['global'])){
			$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_off_depend` (`id`,`off_id`,`parent`,`module`, `off_type`) VALUES ('', 0, ".$parent.", '".$type."', 'off3')", __LINE__, __FILE__);
		}
		if (!isset($off_id['client'])){
			$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_off_depend` (`id`,`off_id`,`parent`,`module`, `off_type`) VALUES ('', 0, ".$parent.", '".$type."', 'off4')", __LINE__, __FILE__);
		}
	}

	function get_off_adv($parent, $type){
		if (isset($this->off_depend['adv'][$type][$parent])) return $this->off_depend['adv'][$type][$parent];
		else return 0;
	}
	
	function get_off($parent, $type){
		if (isset($this->off_depend[$type][$parent])) return $this->off_depend[$type][$parent];
		else return 0;
	}

	function get_price_mod_by_product_id($product_id, $module, $product_parent=NULL){
		if ((isset($product_id)) && (intval($product_id) > 0)){
			if ((isset($this->setupdata['modules']['shop']['modules']['off'])) && ($this->setupdata['modules']['shop']['modules']['off'] == 1)){
				if ($module != 'shop_product') {
					return $this->get_off($product_id, $module);
				} else {
					$price_mod = array();
					$off_product = $this->get_off($product_id, 'shop_product');
					if ($product_parent==NULL){
						$product_data = $this->get_product_fields($product_id);
						$product_parent=$product_data['data']['parent'];
					}
					$off_product_group = $this->get_off($product_parent, 'shop_group');
					$off_product_group_adv = $this->get_off_adv($product_parent, 'shop_group');
					if ($off_product_group_adv == 0) $off_product_group_adv = array();
					
					if (isset($this->off_client['num'])){
						if (array_key_exists('off4', $off_product_group_adv)) $off_client['num'] = 0;
						else $off_client = $this->off_client;
					} else $off_client['num'] = 0;
					
					if (isset($this->offs['global']['num'])){
						if (array_key_exists('off3', $off_product_group_adv)) $off_global['num'] = 0;
						else $off_global = $this->offs['global'];
					} else $off_global['num'] = 0;
					
					if (isset($this->off_select['num'])){
						if (array_key_exists('off2', $off_product_group_adv)) $off_select['num'] = 0;
						else $off_select = $this->off_select;
					} else $off_select['num'] = 0;
					
					$all=@array(
						$off_client['num']=>$off_client,
						$off_product['num']=>$off_product,
						$off_product_group['num']=>$off_product_group,
						$off_global['num']=>$off_global,
						$off_select['num']=>$off_select
					);
					
					krsort($all);
					reset($all);
					$x = current($all);
					if (!isset($x['num'])) return NULL;
					else return $x;
				}
			}
		}
		return 0;
	}
	
	function set_user_permision($user_id, $array){
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."users_depend` WHERE `users_id` = ".$user_id." AND `module` LIKE 'shop_module_%'", __LINE__, __FILE__);							
		if ($array == NULL) return 0;
		foreach ($array as $key=>$item){
			if (isset($this->setupdata['modules']['shop']['modules'][$key]) && ($this->setupdata['modules']['shop']['modules'][$key] == 1)){
				$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."users_depend` (`id`,`page_id`,`users_id`,`module`) VALUES ('', 0, ".$user_id.", 'shop_module_".$key."')", __LINE__, __FILE__);
			} else {
				if ((strpos($key, "group_") !== false) and (strpos($key, "group_") == 0)){
					$k = substr($key, strlen("group_"), strlen($key)-strpos($key, "group_")); 
					$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."users_depend` (`id`,`page_id`,`users_id`,`module`) VALUES ('', ".$k.", ".$user_id.", 'shop_module_group')", __LINE__, __FILE__);
				}
			}
		}
	}
	
	function get_off_global_data(){
		if ((isset($this->setupdata['modules']['shop']['modules']['off'])) && ($this->setupdata['modules']['shop']['modules']['off'] == 1)){
			$price_mod = array();
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_off_groups` WHERE `type`='global' AND `parent`=0", __LINE__, __FILE__);	
			if (mysql_num_rows($sql) > 0) {
				while ($row = mysql_fetch_array($sql, MYSQL_ASSOC))	{
					$result = array();
					$res = array();
					if ($row['type_of_group'] == 'off1') $type = 0;
					elseif ($row['type_of_group'] == 'off2') $type = 1;
					$sql2 = $this->mysqlclass->mysql_my_query("
						SELECT `t1`.`parent` `group_id`, `t1`.`name` `off_name`,`t2`.* FROM `".$this->setupdata['table_prefix']."product_off_groups` `t1`, `".$this->setupdata['table_prefix']."product_off_groups_data` `t2`
						WHERE `t1`.`parent`=".$row['id']." AND `t2`.`parent`=`t1`.`id`
					", __LINE__, __FILE__);
					while ($row2 = mysql_fetch_array($sql2, MYSQL_ASSOC)){
						$res[$row2['parent']]['name'] = stripslashes($row2['off_name']);
						$res[$row2['parent']][$row2['name']] = stripslashes($row2['value']);
						$res[$row2['parent']]['id'] = $row2['parent'];
						$res[$row2['parent']]['parent'] = $row2['group_id'];
					}
					foreach ($res as $result){
						$result['type'] = $type;
						if ((isset($result['desc'])) && (isset($result['num'])) && ((strlen($result['desc']) > 0) && (strlen($result['num']) > 0))){
							$price_mod['all'][$result['type']][] = $result;
							if (isset($price_mod['min'][$result['type']])){
								if ($price_mod['min'][$result['type']]['num'] > $result['num']) $price_mod['min'][$result['type']] = $result;
							} else $price_mod['min'][$result['type']] = $result;
							if (isset($price_mod['max'][$result['type']])){
								if ($price_mod['max'][$result['type']]['num'] < $result['num']) $price_mod['max'][$result['type']] = $result;
							} else $price_mod['max'][$result['type']] = $result;
						}
					}
				}
				return $price_mod;
			}
			return 0;
		}
	}

	function products_search($search, $cache_id="0", $group_id="0", $admin="0"){
		if ($cache_id != "0") {
			$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_search_cache` WHERE `id`=".$cache_id." AND `session_id`='".session_id()."'", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0){
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."product_search_cache` WHERE ".date("YmdHis")."-`date` > 86400", __LINE__, __FILE__);
				$row = mysql_fetch_array($sql, MYSQL_ASSOC);
				if (isset($this->setupdata['cache']['product']['search']['type'])) $row['value'] = gzuncompress($row['value']);
				eval($row['value'].";");
				$results['product_search_cache_id'] = $cache_id;
				$results['products'] = $cache['products'];
				$results['query'] = $cache['query'];
				return $results;
			}				
		}
		for ($i=1; $i <= count($search); $i++) {
			if (isset($search[$i]['type']) && (!is_array($search[$i]['type'])) && (strlen($search[$i]['type'])>0) && ($search[$i]['type']<>"-1") && isset($search[$i]['value']) && (strlen($search[$i]['value'])>0)){
				$search[$i]['value'] = trim($search[$i]['value']);
				$search[$i]['value'] = addslashes($search[$i]['value']);
				if (strlen($search[$i]['value']) > 0){
					if ($search[$i]['type'] == 'product_name'){
						$search_str[] = "(`t1`.`name` LIKE '%".$search[$i]['value']."%')";
					} else {
						$search_str[] = "(`t2`.`name`='".$search[$i]['type']."' AND `t2`.`value` LIKE '%".$search[$i]['value']."%')";
					}
				}
			} elseif (isset($search[$i]['value']) && (strlen($search[$i]['value'])>0) && (!isset($search[$i]['type']))){
				$search[$i]['value'] = trim($search[$i]['value']);
				$search[$i]['value'] = addslashes($search[$i]['value']);
				if (strlen($search[$i]['value']) > 0){
					$search_str[] = "(`t2`.`value` LIKE '%".$search[$i]['value']."%')";
					$search_str[] = "(`t1`.`name` LIKE '%".$search[$i]['value']."%')";
//					$search_str[] = "((`t1`.`name` LIKE '%".$search[$i]['value']."%') OR (`t2`.`value` LIKE '%".$search[$i]['value']."%'))";
				}
			} elseif (isset($search[$i]['type']) && (is_array($search[$i]['type'])) && isset($search[$i]['value']) && (strlen($search[$i]['value'])>0)){
				$search[$i]['value'] = trim($search[$i]['value']);
				$search[$i]['value'] = addslashes($search[$i]['value']);
				if (strlen($search[$i]['value']) > 0){
					$types_str = "";
					foreach ($search[$i]['type'] as $type) {
						if ($types_str == "") $types_str = "(`t2`.`name`='".$type."')";
						else $types_str .= " OR (`t2`.`name`='".$type."')";
					}
					$search_str[] = "(`t2`.`value` LIKE '%".$search[$i]['value']."%' AND (".$types_str."))";
				}
			}
		}
		
		$result_ids = array();
		$result = array();
		
		if (isset($search_str)){
			foreach ($search_str as $search_str_one) {
				if ($group_id != "0") {
					$tmp_search_str_one['query'] = $search_str_one;
					$tmp_search_str_one['group_id'] = $group_id;
					$md = md5(serialize($tmp_search_str_one));
				} else $md = md5(serialize($search_str_one));
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."product_search_cache` WHERE `query`='".$md."'", __LINE__, __FILE__);
				if (mysql_num_rows($sql) > 0){
					$row = mysql_fetch_array($sql, MYSQL_ASSOC);
					if (isset($this->setupdata['cache']['product']['search']['type'])) $row['value'] = gzuncompress($row['value']);
					eval($row['value'].";");
					$result_ids[$i] = $cache;
					unset($cache);
				} else {
					if ($group_id != "0") {
						$group_str = "`t1`.`parent`=".$group_id." AND ";
					} else $group_str = "";
					if ($admin != "0"){
						$str_hide[1] = "";
						$str_hide[2] = "";
						$str_hide[3] = "";
					} else {
						$str_hide[1] = ", `".$this->setupdata['table_prefix']."catalog` `t3`, `".$this->setupdata['table_prefix']."data` `t4`";
						$str_hide[2] = " AND `t1`.`hide`=0";
						$str_hide[3] = " AND (`t1`.`parent`=`t4`.`value` AND `t4`.`name`='shop_products_group_id' AND `t4`.`parent`=`t3`.`id` AND `t3`.`hide` = 1)";
					}
					$q = "
						SELECT DISTINCT `t1`.*, `t2`.`name` `field_name`, `t2`.`parent` `product_id`, `t2`.`value` `product_value`
						FROM `".$this->setupdata['table_prefix']."product_catalog` `t1`, `".$this->setupdata['table_prefix']."product_data` `t2`".$str_hide[1]."
						WHERE ".$group_str."`t1`.`id`=`t2`.`parent`".$str_hide[2]." AND ".$search_str_one.$str_hide[3]."
					";
					if (isset($this->setupdata['cache']['product']['search']['count'])) $q .= "LIMIT 0, ".$this->setupdata['cache']['product']['search']['count']*10;
					$sql = $this->mysqlclass->mysql_my_query($q, __LINE__, __FILE__);
					while ($row = mysql_fetch_array($sql, MYSQL_ASSOC))	{
						$result_ids[$i][$row['product_id']]['product_name'] = $row['name'];
						$result_ids[$i][$row['product_id']][$row['field_name']] = $row['product_value'];
					}
					/*
					if (isset($result_ids[$i])){
						$result_str = "\$cache=";
						$result_str .= var_export($result_ids[$i], true);
						if (isset($this->setupdata['cache']['product']['search']['type'])) $result_str = gzcompress($result_str, 9);
						$result_str = addslashes($result_str);
						$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_search_cache` (`id`,`date`,`value`, `session_id`, `query`) VALUES ('', ".date('YmdHis').", '".$result_str."', '".session_id()."', '".$md."')", __LINE__, __FILE__);
						mysql_free_result($sql);
					}
					*/
				}
			}
		}
		
		$result = $result_ids;
		
		foreach ($result_ids as $i=>$data) {
			if (!isset($data) or ($data == "")) $data = array();
			foreach ($data as $parent=>$value) {
				foreach ($search as $search_one) {
					if (isset($search_one['type']) && ($search_one['type'] != "-1")){
						if (!is_array($search_one['type'])){
							if (isset($search_one['value']) && (strlen($search_one['value']) > 0) && (strlen($search_one['type']) > 0)){
								if (!array_key_exists($search_one['type'], $value)) unset($result[$i][$parent]);
								elseif (stristr($value[$search_one['type']], $search_one['value']) == "") unset($result[$i][$parent]);
							}
						} else {
							if (isset($search_one['value']) && (strlen($search_one['value']) > 0)){
								$eval_atr = "";
								foreach ($search_one['type'] as $type) {
									if ($eval_atr == "") $eval_atr = "(!array_key_exists('".$type."', \$value))";
									else $eval_atr .= " and (!array_key_exists('".$type."', \$value))";
								}
								eval("if ($eval_atr) unset(\$result[\$i][\$parent]);");
							}
						}
					}
				}
			}
		}
	
		
		$result_ids = $result;
		$result = array();
		
		foreach ($result_ids as $i=>$data) {
			if (!isset($data) or ($data == "")) $data = array();
			if (isset($this->setupdata['cache']['product']['search']['count'])){
				if ($this->setupdata['cache']['product']['search']['count'] == 0) break;
			}
			foreach ($data as $parent=>$value) {
				if (isset($this->setupdata['cache']['product']['search']['count'])){
					if ($this->setupdata['cache']['product']['search']['count'] == 0) break;
					else $this->setupdata['cache']['product']['search']['count'] = $this->setupdata['cache']['product']['search']['count'] - 1;
				}
				if (isset($this->setupdata['cache']['product']['search']['fields'])){
					$product_info = $this->get_product_fields_with_data_and_type($parent, $_SESSION['lang']);
					if (isset($product_info['group_id'])){
						$product_info['page_id'] = $this->get_page_id_from_product_group_id($product_info['group_id'], $_SESSION['lang']);
					} else $product_info['page_id'] = 0;
					foreach ($product_info as $product_info_key=>$product_info_data) {
						if (array_key_exists($product_info_key, $this->setupdata['cache']['product']['search']['fields'])) {
							if ($this->setupdata['cache']['product']['search']['fields'][$product_info_key] != 'all'){
								$product_info[$product_info_key]['value'] = substr($product_info_data['value'], 0, $this->setupdata['cache']['product']['search']['fields'][$product_info_key]);
							}
						} else unset($product_info[$product_info_key]);
					}
					if (array_key_exists("price_mod", $this->setupdata['cache']['product']['search']['fields'])) {
						$price_mod = $this->get_price_mod_by_product_id($parent, 'shop_product');
						if ($price_mod != 0) $product_info['price_mod'] = $price_mod;
					}
					if ($admin != "0") $result[$parent] = $product_info;
					else {
						if (($product_info['page_id'] != NULL) or ($product_info['page_id'] != 0)) $result[$parent] = $product_info;
					}
				} else {
					$product_info = $this->get_product_fields_with_data_and_type($parent, $_SESSION['lang']);
					if (isset($product_info['group_id'])){
						$product_info['page_id'] = $this->get_page_id_from_product_group_id($product_info['group_id'], $_SESSION['lang']);
					} else $product_info['page_id'] = 0;
					$price_mod = $this->get_price_mod_by_product_id($parent, 'shop_product');
					if ($price_mod != 0) $product_info['price_mod'] = $price_mod;
					if ($admin != "0") $result[$parent] = $product_info;
					else {
						if (($product_info['page_id'] != NULL) or ($product_info['page_id'] != 0)) $result[$parent] = $product_info;
					}
				}
			}
		}
		
		
		unset($result_ids);
		$tmp['products'] = $result;
		$tmp['query'] = $search;
		unset($result);
		unset($search);
		$result_str = "\$cache=";
		$result_str .= var_export($tmp, true);
		if (isset($this->setupdata['cache']['product']['search']['type'])) $result_str = gzcompress($result_str, 9);
		$result_str = addslashes($result_str);
		$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."product_search_cache` (`id`,`date`,`value`, `session_id`, `query`) VALUES ('', ".date('YmdHis').", '".$result_str."', '".session_id()."', '')", __LINE__, __FILE__);
		$tmp['product_search_cache_id'] = mysql_insert_id();
		return $tmp;
	}

	function product_rename($parent, $name){
		$parent = intval($parent);
		$name = addslashes($name);
		$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_catalog` SET `name`='".$name."' WHERE ".$parent." = `id`", __LINE__, __FILE__);
	}
	
	function off_rename($parent, $name){
		$parent = intval($parent);
		$name = addslashes($name);
		$this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."product_off_groups` SET `name`='".$name."' WHERE ".$parent." = `id`", __LINE__, __FILE__);
	}
	
	function get_page_id_from_product_group_id($group_id, $lang){
		$group_id = intval($group_id);
		$q = "
			SELECT `t1`.`id`
			FROM `".$this->setupdata['table_prefix']."catalog` `t1`, `".$this->setupdata['table_prefix']."data` `t2`
			WHERE `t1`.`type`='shop' AND `t2`.`parent`=`t1`.`id` AND `t2`.`type`='shop_container' AND `t2`.`value`=".$group_id." AND `t1`.`lang`='".$lang."' AND `t1`.`hide`=1"
		;
		$sql = $this->mysqlclass->mysql_my_query($q, __LINE__, __FILE__);
		if (mysql_num_rows($sql) > 0){
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			return $row['id'];
		} else return NULL;
	}
	
	function mod_super_select($parent, $name, $array){
		$selected = $array['selected'];
		$parent = intval($parent);
		unset($array['selected']);
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."object_data` WHERE `name`='".$name."' AND `group`='shop_super_select'", __LINE__, __FILE__);		
		foreach ($array as $value){
			$value['text'] = substr($value['text'], 0, 255); 
			//$value['text'] = addslashes($value['text']);
			$value['text'] = trim($value['text']);
			if ($value['value'] == 'noid'){
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE `name`='".$name."' AND `value`='".$value['text']."' AND `group`='shop_super_select'", __LINE__, __FILE__);
				if (mysql_num_rows($sql) == 0) $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`group`) VALUES ('', ".$parent.", '".$name."', '".$value['text']."', 'text', 0, 'shop_super_select')", __LINE__, __FILE__);
			} else {
				$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE `name`='".$name."' AND `id`=".$value['value']." AND `group`='shop_super_select'", __LINE__, __FILE__);
				if (mysql_num_rows($sql) == 0) $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."object_data` (`id`,`parent`,`name`,`value`,`type`,`pos`,`group`) VALUES ('".$value['value']."', ".$parent.", '".$name."', '".$value['text']."', 'text', 0, 'shop_super_select')", __LINE__, __FILE__);
				elseif (mysql_num_rows($sql) == 1) $sql = $this->mysqlclass->mysql_my_query("UPDATE `".$this->setupdata['table_prefix']."object_data` SET `value`='".$value['text']."', `id`=".$value['value']." WHERE `parent` = ".$parent." AND `name`='".$name."' AND `group`='shop_super_select'", __LINE__, __FILE__);
			}
		}

		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."object_data` WHERE `parent`=".$parent." AND `name`='".$name."' AND `value`='".$selected['text']."' AND `group`='shop_super_select'", __LINE__, __FILE__);
		if (mysql_num_rows($sql) > 0) {
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			$id = $row['id'];
		} else $id = 0;
		
		return $id;
	}
	
	function get_super_select($params){
		$result = array();
		$params['parent'] = intval($params['parent']);
		if (isset($params['name']) && isset($params['parent'])){
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id`, `value` FROM `".$this->setupdata['table_prefix']."object_data` WHERE `name`='".$params['name']."' AND `group`='shop_super_select' ORDER BY `value` ASC", __LINE__, __FILE__);
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
				$result[$row['id']]['text'] = stripslashes($row['value']);
				$result[$row['id']]['value'] = $row['id'];
			}
			$sql = $this->mysqlclass->mysql_my_query("SELECT `value` FROM `".$this->setupdata['table_prefix']."product_data` WHERE `parent`=".$params['parent']." AND `name`='".$params['name']."' AND `type`='super_select'", __LINE__, __FILE__);			
			if (mysql_num_rows($sql) > 0){
				$row = mysql_fetch_array($sql, MYSQL_ASSOC);
				$result['selected']['text'] = stripslashes($result[$row['value']]['text']);
				$result['selected']['value'] = $row['value'];
			} else {
				$result['selected']['text'] = "";
				$result['selected']['value'] = "";
			}
		}
		return $result;
	}
	
	function get_curency_from_lb(){
		if (isset($this->setupdata['vars']['curency']) && ($this->setupdata['vars']['curency']['default'] == 'LTL')){
			$cur = file("http://www.lb.lt/exchange/Results.asp?S=csv&x=".rand(25000, 99999));
			$result['LTL'] = 1;
			foreach ($cur as $value) {
				$tmp = explode(",", $value);
				if (in_array($tmp[1], $this->setupdata['vars']['curency']['all'])){
					$result[$tmp[1]] = $tmp[3]/$tmp[2];
				}
			}
			$this->mod_curency($result);
		}
	}
	
	function get_group_count_data($main_id=0, $sub_id=0){
		$result = array();

		$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."product_catalog`", __LINE__, __FILE__);			
		$result['elements'] = mysql_num_rows($sql);
		$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."product_groups` WHERE `parent`=0", __LINE__, __FILE__);
		$result['groups'] = mysql_num_rows($sql);
		
		if ($main_id > 0){
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."product_groups` WHERE ".$main_id."=`parent`", __LINE__, __FILE__);			
			$result['sub_groups'] = mysql_num_rows($sql);
			$gr = "";
			while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)){
				if ($gr == "") $gr .= "`parent`=".$row['id'];
				else $gr .= " OR `parent`=".$row['id'];
			}
			if ($gr == "") $sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."product_catalog`", __LINE__, __FILE__);			
			else $sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE ".$gr, __LINE__, __FILE__);			
			$result['sub_elements'] = mysql_num_rows($sql);
		}
		
		if ($sub_id > 0){
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."product_catalog` WHERE `parent`=".$sub_id, __LINE__, __FILE__);			
			$result['sub_elements'] = mysql_num_rows($sql);
		}
		
		return $result;
	}

	function add_tree_to_group($parent, $group_parent, $group, $products, $name, $lang){
		$parent = intval($parent);
		$group_parent = intval($group_parent);
		if ($group_parent != "-1"){
			$before = array();
			$info = $this->get_product_field_data($parent, $name, $lang);
			$t1 = explode(";", $info);
			foreach ($t1 as $v1){
				$t2 = explode("=", $v1);
				$t3 = explode(":", $t2[1]);
				$t4 = explode(",", $t3[1]);
				foreach ($t4 as $v4){
					if ((intval($t2[0]) > 0) && (intval($v4) > 0)) $before[$t2[0]][$t3[0]][$v4] = $v4;
				}
			}
			
			if (isset($before[$group_parent][$group])) unset($before[$group_parent][$group]);
			if (isset($before[$group_parent]) && (count($before[$group_parent]) == 0)) unset($before[$group_parent]);
			
			$value = "";
			foreach ($before as $k=>$v){
				foreach ($v as $k1=>$v1){
					$t4 = $k."=".$k1.":";
					foreach ($v1 as $k2=>$v2){
						$t4 .= $k2.",";
					}
					$t4 = substr($t4, 0, strlen($t4)-1).";";
					$value .= $t4;
				}
			}
			
			if (is_array($products) && (count($products) > 0)){
				$value .= $group_parent."=".$group.":";
				foreach ($products as $k=>$v){
					if ($v == 'on') $value .= $k.",";
				}
				$value = substr($value, 0, strlen($value)-1).";";
			}
		} else {
			$value = "";
		}
		$array['name'] = $name;
		$array['type_of_value'] = "shop_subproduct_container";
		$this->mod_product_fields($parent, $array, $value, $lang);
	}
}
?>
